//package com.woniu.gateway.filter;
//
//import cn.hutool.json.JSONUtil;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.hc.entity.User;
//import com.hc.utils.JwtUtil;
//import com.hc.utils.ResponseEntity;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import javax.annotation.Resource;
//import java.nio.charset.StandardCharsets;
//
///**
// * @ClassName AuthorFilter
// * @Description 授权
// * @Author shanshan
// * @Date 2022/10/25 11:54
// * @Version 1.0
// **/
//@Component
//public class AuthorFilter implements GlobalFilter, Ordered {
//
//    private JwtUtil jwtUtil = new JwtUtil();
//
////    @Autowired
////    RedisTemplate<String,Object> redisTemplate;
//
//    @Resource
//    private StringRedisTemplate stringRedisTemplate;
//
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        System.out.println("进入AuthorFilter");
//        ServerHttpRequest request = exchange.getRequest();
//        String login = request.getHeaders().getFirst("login");
//        //判断是否有特定的请求头
//        if(login !=null) {
//            //登录请求直接放行
//            System.out.println("Login请求，直接放行:获取自定义请求头，放行："+request.getHeaders().getFirst("login"));
//            return chain.filter(exchange);
//        }
//
//        //获取请求uri
//        String uri = request.getURI().getPath();
//        System.out.println("uri:"+uri);
//
//        //从token中获取用户roleId
//        String token = request.getHeaders().getFirst("token");
//        //从redis中拿到用户信息 获取权限列表
//        int id = (int)jwtUtil.getInfo(token,"id");
////        System.out.println(redisTemplate.hasKey("user"+id));
////        User user = (User) redisTemplate.opsForValue().get("user"+id);
//        String s = stringRedisTemplate.opsForValue().get("user" + id);
//        User user = JSONUtil.toBean(s, User.class);
//        //判单是否拥有权限
//        boolean match = user.getRole().getPerms()
//                .stream()
//                .anyMatch((perm) -> uri.contains(perm.getUrl()));
//        if(match){
//            //拥有权限放行
//            System.out.println("允许执行，直接放行");
//            return chain.filter(exchange);
//        }
//
//        //没有权限则拦截
//        System.out.println("拦截");
//        return exchange.getResponse().writeWith(Mono.just(result(exchange)));
//
//    }
//
//    //拦截，要求先有权限
//    public DataBuffer result(ServerWebExchange exchange) {
//        ServerHttpResponse response = exchange.getResponse();
//        ResponseEntity responseEntity = new ResponseEntity("403", "no perm", "没有权限");
//        byte[] bytes = null;
//        try {
//            bytes = new ObjectMapper()
//                    .writeValueAsString(responseEntity).getBytes(StandardCharsets.UTF_8);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        DataBuffer dataBuffer = response.bufferFactory().wrap(bytes);
//        response.getHeaders().add("Content-Type", "application/json;charset=utf-8");
//        return dataBuffer;
//    }
//
//    @Override
//    public int getOrder() {
//        return 1;
//    }
//}